using System;
using NHibernate;

namespace TimeTracker.Core.Domain
{
	public class RepositoryBase
	{
		protected ISession NHibernateSession
		{
            get
            {
                return NHibernateSessionManager.Instance.GetSession();
            }
        }
	
		protected void BeginTransaction()
		{
			if (!NHibernateSessionManager.Instance.HasOpenTransaction())
				NHibernateSessionManager.Instance.BeginTransaction();
		}
		
		public void CommitChanges()
		{
            if (NHibernateSessionManager.Instance.HasOpenTransaction())
            {
                try
                {
                    NHibernateSessionManager.Instance.CommitTransaction();
                }
                catch (Exception ex)
                {
                    NHibernateSessionManager.Instance.RollbackTransaction();
                    throw new Exception(ex.Message, ex);
                }
            }
            else
                NHibernateSessionManager.Instance.GetSession().Flush();
        }
	}
}
